package observer;

import java.util.ArrayList;
import java.util.Iterator;

public abstract class Subject {

    private ArrayList<Observer> observers = new ArrayList();

    public void attach(Observer observer) {
        observers.add(observer);
    }

    public void detach(Observer observer) {
        observers.remove(observer);
    }

    public void notifyObservers() {
        Iterator observerIterator = observers.iterator();
        while (observerIterator.hasNext()) {
            Observer observer = (Observer) observerIterator.next();
            observer.update();
        }
    }
}
